package com.usian.wemedia.controller;

import com.usian.api.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.dtos.WmNewsVo;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmNewsService;
import com.usian.wemedia.service.WmUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {
    @Autowired
    private WmNewsService wmNewsService;

    @Autowired

    private WmUserService wmUserService;


    /*
    * 查询
    * */
    @RequestMapping("/list")
    @Override
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }


    /*
    * 添加
    * */
    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {

        if(dto.getStatus()==1){
            //提交文章
            return wmNewsService.submitNews(dto,(short) 1);
        }else{
            //保存草稿
            return wmNewsService.submitNews(dto,(short) 0);
        }
    }

    /*
    * 删除
    * */
     @Override
     @GetMapping("/del_news/{id}")
     public ResponseResult delNews(@PathVariable Integer id) {
         return wmNewsService.delNews(id);
     }

    /**
     * 上下架
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(WmNewsDto dto) {

        return wmNewsService.downOrUp(dto);
    }



    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findByNewsId(@PathVariable Integer id) {
        return wmNewsService.findByNewsId(id);
    }


    /*
    * admin服务远程调用
    * */
    @Override
    @GetMapping("/ones/{id}")
    public WmNews findByNewsIds(@PathVariable Integer id) {
        WmNews wmNews = wmNewsService.getById(id);
        return wmNews;
    }




    /*
    * admin 服务远程调用更新文章
    * */
    @Override
    @PostMapping("/updateNewStatus")
    public ResponseResult updateNewStatus(@RequestBody WmNews wmNews) {
        return wmNewsService.updateNewStatus(wmNews);
    }




    /*
    *
    * admin远程服务根据用户id查询用户对象
    * */

    @Override
    @GetMapping("/findByUserId/{id}")
    public WmUser findByUserId(@PathVariable("id") Integer userId) {
        return wmUserService.findByUserId(userId);
    }


    @Override
    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }


    /**
     * 查询文章列表
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/findList")
    public PageResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }

    /**
     * 查询文章详情
     *
     * @param id
     */
    @Override
    @GetMapping("/find_news_vo/{id}")
    public WmNewsVo findWmNewsVo(@PathVariable Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }
}
